"""
Case Type   : 资源池化
Case Name   : 资源池化场景下, 无任何业务压力, 进行缩容操作
Create At   : 2024-11-04
Owner       : zhangyuemeng
Description :
    1.执行缩容
    2.卸载缩容节点
    3.执行扩容, 恢复环境
Except      :
    1.缩容成功
    2.节点卸载成功
    3.扩容成功
"""

import os
import unittest
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

@unittest.skipIf(3 > Primary_SH.get_node_num(), '节点数量小于3不执行')
class SharedStorage(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_node = Node('PrimaryDbUser')
        self.sta_node = Node('Standby2DbUser')
        self.db_host = self.sta_node.db_host
        self.s_user = self.pri_node.ssh_user
        self.pri_root_node = Node('PrimaryRoot')
        self.sta_root_node = Node('Standby2Root')

    def test_shared_storage(self):
        text = '-----step1: 检查集群状态; expect: 集群状态正常-----'
        self.log.info(text)
        cmd = 'sh /data/yat_log/log.sh'
        pri_res = self.sta_root_node.sh(cmd).result()
        self.log.info(pri_res)

        cmd = f'source {macro.DB_ENV_PATH}; cm_ctl query -Cv'
        res = self.pri_node.sh(cmd).result()
        self.assertIn('cluster_state   : Normal', res, '执行失败' + text)

        text = '-----step2: 执行缩容, 缩容备2节点; expect: 执行成功-----'
        self.log.info(text)
        cmd = f'''source {macro.DB_ENV_PATH};
            expect <<EOF
            set timeout 600
            spawn gs_dropnode -U {self.s_user} -G {self.s_user} \
                -h {self.db_host}
            expect "*drop the target node (yes/no)?*"
            send "yes\\n"
            expect eof\n''' + '''EOF'''
        self.log.info(cmd)
        res = self.pri_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn('[gs_dropnode] Success to drop the target nodes', res)

        text = '-----step3: 卸载缩容节点; expect: 执行成功-----'
        self.log.info(text)
        cmd = f'source {macro.DB_ENV_PATH}; gs_uninstall --delete-data'
        self.log.info(cmd)
        res = self.sta_node.sh(cmd).result()
        self.log.info(res)

        text = '-----step4: 检查集群状态; expect: 集群状态正常-----'
        self.log.info(text)
        cmd = f'source {macro.DB_ENV_PATH}; cm_ctl query -Cv'
        res = self.pri_node.sh(cmd).result()
        self.assertIn('cluster_state   : Normal', res, '执行失败' + text)

        text = '-----step5: 执行扩容, 恢复环境; expect: 执行成功-----'
        self.log.info(text)
        xml_path = macro.DB_XML_PATH
        cmd = f'''gs_expansion -U {self.s_user} -G \
                {self.s_user} -X {xml_path} -h {self.db_host}'''
        execute_cmd = f'''source {macro.DB_ENV_PATH};
            expect <<EOF
            set timeout 300
            spawn {cmd}
            expect "*assword*:"
            send "{self.pri_root_node.ssh_password}\\n"
            expect "*assword*:"
            send "{self.pri_root_node.ssh_password}\\n"
            expect "*lease repeat for database*:"
            send "{macro.GAUSSDB_INIT_USER_PASSWD}\\n"
            expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd)
        res = self.pri_root_node.sh(execute_cmd).result()
        self.log.info(res)
        self.assertIn(f'{self.db_host}:\tSuccess', res)

        text = '-----step6: 检查集群状态; expect: 集群状态正常-----'
        self.log.info(text)
        cmd = f'source {macro.DB_ENV_PATH}; cm_ctl query -Cv'
        res = self.pri_node.sh(cmd).result()
        self.assertIn('cluster_state   : Normal', res, '执行失败' + text)
        cmd = 'sh /data/yat_log/setGuc.sh'
        sta_res = self.sta_root_node.sh(cmd).result()
        self.log.info(sta_res)

    def tearDown(self):
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
